<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>机构权限复制</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">
    <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="css/style.winning.css?v=1.0.0" rel="stylesheet">
    <link rel="stylesheet" href="css/metroStyle/metroStyle.css" type="text/css">
    <link rel="stylesheet" href="css/plugins/table/bootstrap-table.css" type="text/css">
    <link rel="stylesheet" href="css/demo.css?v=1.0.0" type="text/css">


    <style>
        /* ztree通用样式*/
        .ztree-wn * {
            font-family: "微软雅黑", "宋体", Arial, "Times New Roman", Times, serif;
            font-size: 14px;
        }

    </style>
</head>

<body>
<div class="row" style="overflow: auto;height: 370px">
    <div class="col-sm-12">
        <div class="form-group">
            <label id="jgmc" class="col-sm-9 label-normal-wn"></label>
        </div>
    </div>
    <div class="col-sm-12">
        <div class="panel panel-default">
            <div class="panel-heading" align="center">选择机构</div>
            <div class="panel-body">
                <div class="form-group">
                    <div class="col-sm-12">
                        <input id="search" type="text" placeholder="输入机构名称检索" class="form-control">
                    </div>
                </div>
                <div id="tree_jg_copy" class="ztree ztree-wn"></div>
            </div>
        </div>
    </div>
</div>

<footer class="text-center">
    <div class="hr-line-dashed"></div>
    <button class="btn btn-primary-wn" type="button" id="btnSave" onclick="save()"><strong>保存</strong></button>
    <button class="btn btn-normal-wn" type="button" onclick="LayerWn.closeDialog()"><strong>退出</strong></button>
</footer>
<script type="text/javascript" src="js/jquery.min.js?v=3.5.1"></script>
<script type="text/javascript" src="js/bootstrap.min.js?v=3.3.6"></script>
<script type="text/javascript" src="js/content.min.js?v=1.0.0"></script>
<script type="text/javascript" src="js/jquery.ztree.core.js?v=3.5"></script>
<script type="text/javascript" src="js/jquery.ztree.excheck.js?v=3.5"></script>
<script type="text/javascript" src="js/plugins/jsTree/jstree.min.js"></script>
<script type="text/javascript" src="js/plugins/table/bootstrap-table.js"></script>
<script type="text/javascript" src="js/plugins/table/locale/bootstrap-table-zh-CN.js"></script>
<script type="text/javascript" src="js/plugins/layer/layer.min.js"></script>
<script type="text/javascript" src="platcommon/common.js"></script>
<script type="text/javascript" src="platcommon/layer-wn.js"></script>
<script type="text/javascript" src="js/jquery.ztree.exhide.js?v=1.0.0"></script>
<script type="text/javascript" src="js/fuzzysearch.js?v=1.0.0"></script>
<script>
    $(document).ready(function () {
        $.fn.zTree.init($("#tree_jg_copy"), setting, zNodes_jg_copy);
        getJgInfo();
        getJgTreeToCopy();
        // $("[id*='tree']").css("font-size", "13px");
        fuzzySearch('tree_jg_copy', '#search', null, true); //初始化模糊搜索方法

    });

    //**********************左侧树形开始

    var setting = {
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false
        },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "", "N": ""}
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: jgTreeOnClick,
            onExpand: zTreeOnExpand
      }

    };

    var zNodes_jg_copy = [{}];

    //**********************左侧树形end
    //***************************ztree 点击样式 *************
    var newCount = 1;
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click",
            function () {
                var zTree = $.fn.zTree.getZTreeObj("tree_jg_copy");
                zTree.addNodes(treeNode, {
                    id: (100 + newCount),
                    pId: treeNode.id,
                    name: "new node" + (newCount++)
                });
                return false;
            });
    }
    ;

    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    }
    ;

    //***************************ztree 点击样式 *************
    /**
     * 定义树节点展开事件，查询子节点
     * @param event
     * @param treeId
     * @param treeNode
     */
    function zTreeOnExpand(event, treeId, treeNode){
        queryChildrenList(treeNode);
    }

    /**
     * 查询子节点
     * @param treeNode
     */
    function queryChildrenList(treeNode){
        var jgmc = $("#search").val();
        if(jgmc != '' || treeNode.children != undefined){
            return;
        }
        var queryParms = {
            pid : treeNode.id,
            jgmc : jgmc
        };
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "commonapi/findjgtreeByPid",
            data: queryParms,
            success:function(data){
                //添加子节点到指定的父节点
                var jsondata= eval(data.jgTreeList);
                for(var i=0;i<jsondata.length;i++){
                    if(jsondata[i].isLeaf != '1'){
                        jsondata[i].isParent = true;
                    }
                }
                if(jsondata != null && jsondata != ""){
                    var treeObj = $.fn.zTree.getZTreeObj("tree_jg_copy");
                    var parentZNode = treeObj.getNodeByParam("id", treeNode.id, null);
                    treeObj.addNodes(parentZNode,jsondata, false);
                }
            },
            error: function () {
                alert('请求失败');
            }
        });
    }



    /**
     * 获取机构名称
     */
    function getJgInfo() {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "commonapi/jginfo/" + Common.GetRequest().jgbm,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    $('#jgmc').html('机构名称:' + jsonData.syszcjgeneity.jgmc);

                }
            }
        });
    }


    /**
     * 获取当前机构树形
     */
    function getJgTreeToCopy() {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "commonapi/findjgtree",
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    var jgTreeList = jsonData.jgTreeList;
                    for(var i=0;i<jgTreeList.length;i++){
                        if(jgTreeList[i].isLeaf != '1'){
                            jgTreeList[i].isParent = true;
                        }
                    }

                    $.fn.zTree.init($("#tree_jg_copy"), setting, jsonData.jgTreeList);
                    var treeObj = $.fn.zTree.getZTreeObj("tree_jg_copy");
                    var nodes = treeObj.getNodes();
                    if (nodes.length > 0) {
                        //默认展开一级节点
                        treeObj.expandNode(nodes[0], true, false, true);
                    }
                } else {
                    LayerWn.showFailAlert("获取机构错误");
                }
            }
        });
    }

    /**
     * 机构树形选中
     */

    function jgTreeOnClick(event, treeId, treeNode, clickFlag) {
        //重新渲染机构权限树
       // getCheckZcjgQx(treeNode.id);
        // var treeObj = $.fn.zTree.getZTreeObj("tree_jgqx");
        // treeObj.cancelSelectedNode();
        jgbm = treeNode.id;
    }

    /**
     *  保存 机构权限复制
     */
    function save() {

        if (validate(onCheckZtree())) {
            $("#btnSave").attr('disabled', true);
            $.ajax({
                type: "POST",
                contentType: 'application/x-www-form-urlencoded',
                dataType: "json",
                url: "role/copyjgqx",
                data: {
                    jgbm: Common.GetRequest().jgbm,
                    ztreeCheckList: onCheckZtree()

                },
                success: function (jsonData) {
                    $("#btnSave").removeAttr('disabled');
                    if (jsonData.code == 200) {   //查询成功
                        LayerWn.showSucAlert("复制机构权限成功");
                        LayerWn.closeDialog();

                    }
                }
            });
        }
    }

    //提交前校验
    function validate(ztreelist) {

        if (ztreelist.length == 0) {
            LayerWn.showAlert('请选择机构！');
            return false;
        }
        return true;
    }

    //获取机构树选中
    function onCheckZtree() {
        var treeObj = $.fn.zTree.getZTreeObj("tree_jg_copy"),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        for (var i = 0; i < nodes.length; i++) {
            v += nodes[i].id + "|";
        }
        return v;

    }

</script>


</body>

</html>